home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GXHINT2.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  12KB  |  363 lines

  1. /* Copyright (C) 1990, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxhint2.c */
  20. /* Character level hints for Type 1 fonts. */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxarith.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gzstate.h"
  27. #include "gzdevice.h"            /* for gxchar */
  28. #include "gxdevmem.h"            /* ditto */
  29. #include "gxchar.h"
  30. #include "gxfont.h"
  31. #include "gxtype1.h"
  32. #include "gxop1.h"
  33.  
  34. /* Define the tolerance for testing whether a point is in a zone, */
  35. /* in device pixels.  (Maybe this should be variable?) */
  36. #define stem_tolerance float2fixed(0.05)
  37.  
  38. /* Forward references */
  39.  
  40. private stem_hint *near type1_stem(P3(stem_hint_table *, fixed, fixed));
  41. private fixed near find_snap(P3(fixed, const stem_snap_table *, const pixel_scale *));
  42. private alignment_zone *near find_zone(P3(gs_type1_state *, fixed, fixed));
  43.  
  44. /* Reset the stem hints. */
  45. void
  46. reset_stem_hints(register gs_type1_state *pis)
  47. {    pis->hstem_hints.count = 0;
  48.     pis->hstem_hints.current = &pis->hstem_hints.data[0];
  49.     pis->vstem_hints.count = 0;
  50.     pis->vstem_hints.current = &pis->vstem_hints.data[0];
  51. }
  52.  
  53. /* ------ Add hints ------ */
  54.  
  55. #define c_fixed(d, c) m_fixed(d, c, pis->fc, max_coeff_bits)
  56.  
  57. /* Add a horizontal stem hint. */
  58. void
  59. type1_hstem(register gs_type1_state *pis, fixed y, fixed dy)
  60. {    stem_hint *psh;
  61.     alignment_zone *pz;
  62.     fixed v, dv, adj_dv;
  63.     fixed vtop, vbot;
  64.     fixed center, diff_v, diff2_dv;
  65.     if ( !pis->fh.use_y_hints ) return;
  66.     y += pis->lsb.y;
  67.     if ( pis->fh.axes_swapped )
  68.         v = pis->vs_offset.x + c_fixed(y, yx) +
  69.             pis->pgs->ctm.tx_fixed,
  70.         dv = c_fixed(dy, yx);
  71.     else
  72.         v = pis->vs_offset.y + c_fixed(y, yy) +
  73.             pis->pgs->ctm.ty_fixed,
  74.         dv = c_fixed(dy, yy);
  75.     if ( dy < 0 )
  76.         vbot = v + dv, vtop = v;
  77.     else
  78.         vbot = v, vtop = v + dv;
  79.     if ( dv < 0 ) v += dv, dv = -dv;
  80.     psh = type1_stem(&pis->hstem_hints, v, dv);
  81.     if ( psh == 0 ) return;
  82.     adj_dv = find_snap(dv, &pis->fh.snap_h, &pis->scale);
  83.     pz = find_zone(pis, vbot, vtop);
  84.     if ( pz != 0 )
  85.     {    /* Use the alignment zone to align the outer stem edge. */
  86.         int inverted =
  87.           (pis->fh.axes_swapped ? pis->fh.x_inverted : pis->fh.y_inverted);
  88.         int adjust_v1 =
  89.           (inverted ? !pz->is_top_zone : pz->is_top_zone);
  90.         fixed flat_v = pz->flat;
  91.         fixed overshoot =
  92.             (pz->is_top_zone ? vtop - flat_v : flat_v - vbot);
  93.         fixed pos_over =
  94.             (inverted ? -overshoot : overshoot);
  95.         fixed ddv = adj_dv - dv;
  96.         fixed shift = scaled_rounded(flat_v, &pis->scale) - flat_v;
  97.         if ( pos_over > 0 )
  98.         {
  99.           if ( pos_over < pis->fh.blue_shift || pis->fh.suppress_overshoot )
  100.           {    /* Character is small, suppress overshoot. */
  101.             if_debug0('y', "[y]suppress overshoot\n");
  102.             if ( pz->is_top_zone )
  103.                 shift -= overshoot;
  104.             else
  105.                 shift += overshoot;
  106.           }
  107.           else
  108.           if ( pos_over < pis->scale.unit )
  109.           {    /* Enforce overshoot. */
  110.             if_debug0('y', "[y]enforce overshoot\n");
  111.             if ( overshoot < 0 )
  112.                 overshoot = -pis->scale.unit - overshoot;
  113.             else
  114.                 overshoot = pis->scale.unit - overshoot;
  115.             if ( pz->is_top_zone )
  116.                 shift += overshoot;
  117.             else
  118.                 shift -= overshoot;
  119.           }
  120.         }
  121.         if ( adjust_v1 )
  122.             psh->dv1 = shift, psh->dv0 = shift - ddv;
  123.         else
  124.             psh->dv0 = shift, psh->dv1 = shift + ddv;
  125.         if_debug4('y', "[y]flat_v = %g, overshoot = %g, dv=%g,%g\n",
  126.               fixed2float(flat_v), fixed2float(overshoot),
  127.               fixed2float(psh->dv0), fixed2float(psh->dv1));
  128.         return;
  129.     }
  130.     /* Align the stem so its edges fall on pixel boundaries, */
  131.     /* moving the center as little as possible. */
  132.     center = v + arith_rshift_1(dv);
  133.     if ( adj_dv & pis->scale.unit )
  134.     {    /* Odd width, align center on half-pixel. */
  135.         center += pis->scale.half;
  136.     }
  137.     diff_v = scaled_rounded(center, &pis->scale) - center;
  138.     diff2_dv = arith_rshift_1(adj_dv - dv);
  139.     psh->dv0 = diff_v - diff2_dv;
  140.     psh->dv1 = diff_v + diff2_dv;
  141.     if_debug6('y', "[y]hstem %g,%g -> %g,%g ; d = %g,%g\n",
  142.           fixed2float(y), fixed2float(dy),
  143.           fixed2float(v), fixed2float(dv),
  144.           fixed2float(psh->dv0), fixed2float(psh->dv1));
  145. }
  146.  
  147. /* Add a vertical stem hint. */
  148. void
  149. type1_vstem(register gs_type1_state *pis, fixed x, fixed dx)
  150. {    stem_hint *psh;
  151.     fixed v, dv, adj_dv;
  152.     fixed center, diff_v, diff2_dv;
  153.     if ( !pis->fh.use_x_hints ) return;
  154.     x += pis->lsb.x;
  155.     if ( pis->fh.axes_swapped )
  156.         v = pis->vs_offset.y + c_fixed(x, xy) +
  157.             pis->pgs->ctm.ty_fixed,
  158.         dv = c_fixed(dx, xy);
  159.     else
  160.         v = pis->vs_offset.x + c_fixed(x, xx) +
  161.             pis->pgs->ctm.tx_fixed,
  162.         dv = c_fixed(dx, xx);
  163.     if ( dv < 0 ) v += dv, dv = -dv;
  164.     psh = type1_stem(&pis->vstem_hints, v, dv);
  165.     if ( psh == 0 ) return;
  166.     adj_dv = find_snap(dv, &pis->fh.snap_v, &pis->scale);
  167.     if ( pis->pdata->ForceBold && adj_dv < pis->scale.unit )
  168.         adj_dv = pis->scale.unit;
  169.     /* Align the stem so its edges fall on pixel boundaries, */
  170.     /* moving the center as little as possible. */
  171.     center = v + arith_rshift_1(dv);
  172.     if ( adj_dv & pis->scale.unit )
  173.     {    /* Odd width, align center on half-pixel. */
  174.         center += pis->scale.half;
  175.     }
  176.     diff_v = scaled_rounded(center, &pis->scale) - center;
  177.     diff2_dv = arith_rshift_1(adj_dv - dv);
  178.     psh->dv0 = diff_v - diff2_dv;
  179.     psh->dv1 = diff_v + diff2_dv;
  180.     if_debug6('y', "[y]vstem %g,%g -> %g,%g ; d = %g,%g\n",
  181.           fixed2float(x), fixed2float(dx),
  182.           fixed2float(v), fixed2float(dv),
  183.           fixed2float(psh->dv0), fixed2float(psh->dv1));
  184. }
  185.  
  186. /* Adjust the character center for a vstem3. */
  187. /****** NEEDS UPDATING FOR SCALE ******/
  188. void
  189. center_vstem(gs_type1_state *pis, fixed x0, fixed dx)
  190. {    fixed x1 = x0 + dx;
  191.     gs_fixed_point pt0, pt1, width;
  192.     fixed center, int_width;
  193.     fixed *psxy;
  194.     gs_point_transform2fixed(&pis->pgs->ctm,
  195.         fixed2float(x0), 0.0, &pt0);
  196.     gs_point_transform2fixed(&pis->pgs->ctm,
  197.         fixed2float(x1), 0.0, &pt1);
  198.     width.x = pt0.x - pt1.x;
  199.     if ( width.x < 0 ) width.x = - width.x;
  200.     width.y = pt0.y - pt1.y;
  201.     if ( width.y < 0 ) width.y = - width.y;
  202.     if ( width.y < float2fixed(0.05) )
  203.     {    /* Vertical on device */
  204.         center = arith_rshift_1(pt0.x + pt1.x);
  205.         int_width = fixed2int_rounded(width.x);
  206.         psxy = &pis->vs_offset.x;
  207.     }
  208.     else
  209.     {    /* Horizontal on device */
  210.         center = arith_rshift_1(pt0.y + pt1.y);
  211.         int_width = fixed2int_rounded(width.y);
  212.         psxy = &pis->vs_offset.y;
  213.     }
  214.     if ( int_width == 0 || (int_width & 1) == 0 )
  215.     {    /* Odd width, center stem over pixel. */
  216.         *psxy = fixed_floor(center) + fixed_half - center;
  217.     }
  218.     else
  219.     {    /* Even width, center stem between pixels. */
  220.         *psxy = fixed_rounded(center) - center;
  221.     }
  222.     /* We can't fix up the current point here, */
  223.     /* but we can fix up everything else. */
  224.     /****** TO BE COMPLETED ******/
  225. }
  226.  
  227. /* Add a stem hint, keeping the table sorted. */
  228. /* Return the stem hint pointer, or 0 if the table is full. */
  229. private stem_hint *near
  230. type1_stem(stem_hint_table *psht, fixed v0, fixed d)
  231. {    stem_hint *bot = &psht->data[0];
  232.     stem_hint *top = bot + psht->count;
  233.     if ( psht->count >= max_stems ) return 0;
  234.     while ( top > bot && v0 < top[-1].v0 )
  235.        {    *top = top[-1];
  236.         top--;
  237.        }
  238.     /* Add a little fuzz for insideness testing. */
  239.     top->v0 = v0 - stem_tolerance;
  240.     top->v1 = v0 + d + stem_tolerance;
  241.     psht->count++;
  242.     return top;
  243. }
  244.  
  245. /* Compute the adjusted width of a stem. */
  246. /* The value returned is always a multiple of scale.unit. */
  247. private fixed near
  248. find_snap(fixed dv, const stem_snap_table *psst, const pixel_scale *pps)
  249. {    fixed best = pps->half;
  250.     fixed adj_dv;
  251.     int i;
  252.     for ( i = 0; i < psst->count; i++ )
  253.     {    fixed diff = psst->data[i] - dv;
  254.         if ( any_abs(diff) < any_abs(best) )
  255.         {    if_debug3('Y', "[Y]possibly snap %g to [%d]%g\n",
  256.                   fixed2float(dv), i,
  257.                   fixed2float(psst->data[i]));
  258.             best = diff;
  259.         }
  260.     }
  261.     adj_dv = scaled_rounded((any_abs(best) < pps->half ? dv + best : dv),
  262.                 pps);
  263.     if ( adj_dv == 0 )
  264.         adj_dv = pps->unit;
  265. #ifdef DEBUG
  266.     if ( adj_dv == dv )
  267.         if_debug1('Y', "[Y]no snap %g\n", fixed2float(dv));
  268.     else
  269.         if_debug2('Y', "[Y]snap %g to %g\n",
  270.               fixed2float(dv), fixed2float(adj_dv));
  271. #endif
  272.     return adj_dv;
  273. }
  274.  
  275. /* Find the applicable alignment zone for a stem, if any. */
  276. /* vbot and vtop are the bottom and top of the stem, */
  277. /* but without interchanging if the y axis is inverted. */
  278. private alignment_zone *near
  279. find_zone(gs_type1_state *pis, fixed vbot, fixed vtop)
  280. {    alignment_zone *pz;
  281.     for ( pz = &pis->fh.a_zones[pis->fh.a_zone_count];
  282.           --pz >= &pis->fh.a_zones[0];
  283.         )
  284.     {    fixed v = (pz->is_top_zone ? vtop : vbot);
  285.         if ( v >= pz->v0 && v <= pz->v1 )
  286.         {    if_debug2('Y', "[Y]stem crosses %s-zone %d\n",
  287.                   (pz->is_top_zone ? "top" : "bottom"),
  288.                   (int)(pz - &pis->fh.a_zones[0]));
  289.             return pz;
  290.         }
  291.     }
  292.     return 0;
  293. }
  294.  
  295. /* ------ Apply hints ------ */
  296.  
  297. private stem_hint *near search_hints(P2(stem_hint_table *, fixed));
  298.  
  299. /* Adjust a point according to the relevant hints. */
  300. /* x and y are the current point in device space after moving; */
  301. /* dx and dy are the delta components in character space. */
  302. void
  303. find_stem_hints(gs_type1_state *pis, fixed x, fixed y,
  304.   fixed dx, fixed dy, gs_fixed_point *ppt)
  305. {    ppt->x = x, ppt->y = y;
  306.     if ( pis->in_dotsection ) return;
  307.     /* Note that if use_x/y_hints is false, */
  308.     /* no entries ever get made in the stem hint tables, */
  309.     /* so we don't have to check those flags here. */
  310.     /* Check the vertical stem hints. */
  311.     if ( pis->vstem_hints.count )
  312.     {    fixed *pv = (pis->fh.axes_swapped ? &ppt->y : &ppt->x);
  313.         stem_hint *ph = search_hints(&pis->vstem_hints, *pv);
  314.         if ( ph != 0 )
  315.         {    /* Decide which side of the stem we are on. */
  316.             /* If we're moving horizontally, just use the */
  317.             /* x coordinate; otherwise, assume outside */
  318.             /* edges move clockwise and inside edges move */
  319.             /* counter-clockwise.  (This algorithm was */
  320.             /* taken from the IBM X11R5 rasterizer; I'm not */
  321.             /* sure I believe it.) */
  322. #define adjust_stem(pv, dxy, ph, inverted)\
  323.   *pv +=\
  324.     ((/*dxy == 0 ?*/ *pv < arith_rshift_1(ph->v0 +\
  325.       ph->v1) /*: inverted ? dxy < 0 : dxy > 0*/) ?\
  326.      ph->dv0 : ph->dv1)
  327.             if_debug2('Y', "[Y]use vstem %d: %g",
  328.                   (int)(ph - &pis->vstem_hints.data[0]),
  329.                   fixed2float(*pv));
  330.             adjust_stem(pv, dy, ph, pis->fh.y_inverted);
  331.             if_debug1('Y', " -> %g\n", fixed2float(*pv));
  332.         }
  333.     }
  334.     /* Check the horizontal stem hints. */
  335.     if ( pis->hstem_hints.count )
  336.     {    fixed *pv = (pis->fh.axes_swapped ? &ppt->x : &ppt->y);
  337.         stem_hint *ph = search_hints(&pis->hstem_hints, *pv);
  338.         if ( ph != 0 )
  339.         {    if_debug2('Y', "[Y]use hstem %d: %g",
  340.                   (int)(ph - &pis->hstem_hints.data[0]),
  341.                   fixed2float(*pv));
  342.             adjust_stem(pv, dx, ph, pis->fh.x_inverted);
  343.             if_debug1('Y', " -> %g\n", fixed2float(*pv));
  344.         }
  345.     }
  346.     return;
  347. #undef adjust_stem
  348. }
  349.  
  350. /* Search one hint table for an adjustment. */
  351. private stem_hint *near
  352. search_hints(stem_hint_table *psht, fixed v)
  353. {    stem_hint *ph = psht->current;
  354.     if ( v >= ph->v0 && v <= ph->v1 ) return ph;
  355.     /* We don't bother with binary or even up/down search, */
  356.     /* because there won't be very many hints. */
  357.     for ( ph = &psht->data[psht->count]; --ph >= &psht->data[0]; )
  358.       if ( v >= ph->v0 && v <= ph->v1 )
  359.         return (psht->current = ph);
  360.     return 0;
  361. }
  362.  
  363.